package com.wangsd.web.face.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wangsd.web.face.entity.TradeNo;
import com.wangsd.web.face.mapper.TradeNoMapper;
import com.wangsd.web.face.service.TradeNoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author generator
 * @since 2019-12-11
 */
@Service
@Slf4j
public class TradeNoServiceImpl extends ServiceImpl<TradeNoMapper, TradeNo> implements TradeNoService {

    private static final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyMMdd");

    private ReentrantLock lock = new ReentrantLock();
    @Value("${spring.profiles.active}")
    private String activeType;
    @Resource
    TradeNoMapper tradeNoMapper;

    @Override
    public String getTradeNo() {
        String tradeNo = null;
        try {
            // 共享读锁
            lock.lock();
            String beforeTradeNo = simpleDateFormat.format(new Date());
            SimpleDateFormat nowSimpleDateFormat = new SimpleDateFormat("yyyyMMdd");
            String nowDate = nowSimpleDateFormat.format(new Date());
            List<TradeNo> tradeNos = tradeNoMapper.selectList(new QueryWrapper<TradeNo>().eq("now_date", nowDate).orderByDesc("after_no"));
            String afterTradeNo = "";
            DecimalFormat df = new DecimalFormat("00000000");
            Long afterNo ;
            if (tradeNos != null && tradeNos.size() > 0) {
                afterNo = tradeNos.get(0).getAfterNo();
                afterNo = ++afterNo;
                afterTradeNo = df.format(afterNo);
                tradeNoMapper.deleteById(tradeNos.get(0).getId());
            } else {
                afterNo = 1L;
                afterTradeNo = df.format(1L);
            }
            TradeNo tradeNo1 = new TradeNo();
            tradeNo1.setAfterNo(afterNo);
            tradeNo1.setNowDate(nowDate);
            tradeNoMapper.insert(tradeNo1);
            tradeNo = beforeTradeNo + afterTradeNo;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
        if(StringUtils.isNotBlank(activeType) && (activeType.equals("dev") || activeType.equals("test") )){
            log.info("读取到的启动环境："+activeType);
            return "0"+System.currentTimeMillis();
        }else{
            return tradeNo;
        }
    }
}
